﻿using System.Collections.Generic;

namespace Borg.Framework
{
    internal class ReleaseCommand : ICommand
    {
        public IEnumerable<string> GetArguments(string[] command, int pos)
        {
            switch (pos)
            {
                case 0:
                    return new[] { "ExtensionInfo" };

                case 1:
                    var args = new string[Runtime.Repository.ExtensionInfos.Count];

                    for (var i = 0; i < args.Length; i++)
                        args[i] = Runtime.Repository.ExtensionInfos[i].Name;

                    return args;
            }

            return null;
        }

        private static string Release(string[] command)
        {
            string str;

            if (command.Length < 2)
                return "Too few arguments.";

            if (command[1] != "ExtensionInfo")
                return "ExtensionInfo argument expected.";

            if (command.Length < 3)
                return "Too few arguments.";

            var elements = RuntimeCommands.Find(command, 1, out str);

            if (elements.Length == 0)
                return str;

            foreach (ExtensionInfo ei in elements)
                ei.Release();

            return string.Empty;
        }

        public CommandHandler Handler
        {
            get { return Release; }
        }

        public string Help
        {
            get { return "Help for command \"release\""; }
        }

        public string Name
        {
            get { return "release"; }
        }
    }
}

